home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / handyscrollingsample / sampleutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  10.1 KB  |  282 lines

  1. /*
  2.     File: SampleUtils.c
  3.     
  4.     Description:
  5.         This file contains a number of utility routines used by this
  6.     sample application.  These routines have been moved
  7.     here to a separate file to simplify the example.
  8.  
  9.     Copyright:
  10.         © Copyright 2000 Apple Computer, Inc. All rights reserved.
  11.     
  12.     Disclaimer:
  13.         IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  14.         ("Apple") in consideration of your agreement to the following terms, and your
  15.         use, installation, modification or redistribution of this Apple software
  16.         constitutes acceptance of these terms.  If you do not agree with these terms,
  17.         please do not use, install, modify or redistribute this Apple software.
  18.  
  19.         In consideration of your agreement to abide by the following terms, and subject
  20.         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  21.         copyrights in this original Apple software (the "Apple Software"), to use,
  22.         reproduce, modify and redistribute the Apple Software, with or without
  23.         modifications, in source and/or binary forms; provided that if you redistribute
  24.         the Apple Software in its entirety and without modifications, you must retain
  25.         this notice and the following text and disclaimers in all such redistributions of
  26.         the Apple Software.  Neither the name, trademarks, service marks or logos of
  27.         Apple Computer, Inc. may be used to endorse or promote products derived from the
  28.         Apple Software without specific prior written permission from Apple.  Except as
  29.         expressly stated in this notice, no other rights or licenses, express or implied,
  30.         are granted by Apple herein, including but not limited to any patent rights that
  31.         may be infringed by your derivative works or by other works in which the Apple
  32.         Software may be incorporated.
  33.  
  34.         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  35.         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  36.         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  37.         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  38.         COMBINATION WITH YOUR PRODUCTS.
  39.  
  40.         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  41.         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  42.         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  44.         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  45.         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  46.         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  
  48.     Change History (most recent first):
  49.         Tue, Feb 8, 2000 -- created
  50. */
  51.  
  52.  
  53. #include "SampleUtils.h"
  54.  
  55. #include <Processes.h>
  56. #include <string.h>
  57. #include <QuickDraw.h>
  58. #include <Gestalt.h>
  59. #include <Palettes.h>
  60. #include <Resources.h>
  61. #include <Appearance.h>
  62.  
  63.  
  64.  
  65. /* SetWindowStandardStateSize sets the window's standard state rectangle
  66.     to a rectangle of the size suggested in the width and height parameters.
  67.     In the end, it may set the standard size to something smaller than the
  68.     width and height parameters so the entire window remains visible.  The
  69.     standard rectangle is also centered on the main screen.  The window's
  70.     standard state rectangle is used by ZoomWindow whenever when it
  71.     is called with the inZoomOut partcode. */
  72. void SetWindowStandardStateSize(WindowPtr window, short width, short height) {
  73.     Rect standard, stdRect, global, device, c, s, diffs;
  74.     RgnHandle tempRgn;
  75.     GDHandle theDevice;
  76.     CGrafPtr wport;
  77.     Point origin;
  78.         /* make the window's port the current port */
  79.     wport = GetWindowPort(window);
  80.     SetPort(wport);
  81.     SetPt(&origin, 0, 0);
  82.     LocalToGlobal(&origin);
  83.         /* find the window's global coordinates */
  84.     GetPortBounds(wport, &global);
  85.     OffsetRect(&global, origin.h, origin.v);
  86.         /* get the maximum intersecting screen */
  87.     theDevice = GetMaxDevice(&global);
  88.     device = (**theDevice).gdRect;
  89.         /* if it's the main screen, adjust it for the menu bar height */
  90.     if (theDevice == GetMainDevice())
  91.         device.top += GetMBarHeight();
  92.         /* calculate the difference between the window's content rectangle and its frame */
  93.     tempRgn = NewRgn();
  94.     GetWindowRegion(window, kWindowContentRgn, tempRgn);
  95.     GetRegionBounds(tempRgn, &c);
  96.     GetWindowRegion(window, kWindowStructureRgn, tempRgn);
  97.     GetRegionBounds(tempRgn, &s);
  98.     DisposeRgn(tempRgn);
  99.     SetRect(&diffs, c.left - s.left + 2, c.top - s.top + 2, s.right - c.right + 2, s.bottom - c.bottom + 2);
  100.         /* calculate the maximum bounds for the standard rectangle */
  101.     SetRect(&standard, device.left + diffs.left, device.top + diffs.top, device.right - diffs.right, device.bottom - diffs.bottom);
  102.         /* set up our 'desired' rectangle */
  103.     SetRect(&stdRect, 0, 0, width, height);
  104.     OffsetRect(&stdRect, standard.left, standard.top);
  105.         /* fit it inside of the maximum allowable rectangle */
  106.     SectRect(&stdRect, &standard, &stdRect);
  107.         /* center it in the maximum allowable rectangle */
  108.     OffsetRect(&stdRect, (standard.right - stdRect.right)/2, (standard.bottom - stdRect.bottom)/2);
  109.         /* set the standard state rectangle */
  110.     SetWindowStandardState(window, &stdRect);
  111. }
  112.  
  113. #include <StdIO.h>
  114. #include <StdArg.h>
  115. #include <TextUtils.h>
  116.  
  117.  
  118.  
  119.  
  120. /* GetApplicationFolder returns the volume reference number and
  121.     directory id of the folder containing the application. */
  122. OSStatus GetApplicationFolder(FSSpec *spec) {
  123.     OSStatus err;
  124.     FCBPBRec fcpb;
  125.     Str255 name;
  126.     CInfoPBRec cat;
  127.     Handle h;
  128.     
  129.     h = GetResource('vTeZ', 0);
  130.     if (h == NULL) return resNotFound;
  131.     
  132.     memset(&fcpb, 0, sizeof(fcpb));
  133.     fcpb.ioVRefNum = 0;
  134.     fcpb.ioNamePtr = name;
  135.     fcpb.ioFCBParID = 0;
  136.     fcpb.ioRefNum = HomeResFile(h);
  137.     fcpb.ioFCBIndx = 0;
  138.     if ((err = PBGetFCBInfoSync(&fcpb)) != noErr) return err;
  139.     
  140.     memset(&cat, 0, sizeof(cat));
  141.     cat.dirInfo.ioNamePtr = name;
  142.     cat.dirInfo.ioVRefNum = fcpb.ioFCBVRefNum;
  143.     cat.dirInfo.ioFDirIndex = -1;
  144.     cat.dirInfo.ioDrDirID = fcpb.ioFCBParID;
  145.     err = PBGetCatInfoSync(&cat);
  146.     if (err != noErr) return err;
  147.     
  148.     err = FSMakeFSSpec(fcpb.ioFCBVRefNum, cat.dirInfo.ioDrParID, name, spec);
  149.     if (err != noErr) return err;
  150.     
  151.     return noErr;
  152. }
  153.  
  154. /* DrawGrowIconWithoutScrollLines draws the grow icon in the bottom
  155.     right corner of the frontmost window without drawing the scroll bar
  156.     lines.  It does this by setting the clip region to the bottom right corner
  157.     before calling DrawGrowIcon. */
  158. void DrawGrowIconWithoutScrollLines(WindowPtr window) {
  159.     RgnHandle clip;
  160.     CGrafPtr wport;
  161.     Rect r = { 0, 0, 16, 16 }, pbox;
  162.         /* set the window to the current port */
  163.     wport = GetWindowPort(window);
  164.     SetPort(wport);
  165.     GetPortBounds(wport, &pbox);
  166.         /* save the old clipping region */
  167.     clip = NewRgn();
  168.     if (clip == NULL) return;
  169.     GetClip(clip);
  170.         /* set the clipping region to the bottom right corner of the window */
  171.     OffsetRect(&r, pbox.right-15, pbox.bottom-15);
  172.     ClipRect(&r);
  173.         /* draw the grow icon */
  174.     DrawGrowIcon(window);
  175.     SetPort(wport);
  176.     SetClip(clip);
  177.     DisposeRgn(clip);
  178. }
  179.  
  180.  
  181.  
  182.  
  183. /* GrayOutBox grays out an area of the screen in the current grafport.  
  184.     *theBox is in local coordinates in the current grafport. This routine
  185.     is for direct screen drawing only.  */
  186. void GrayOutBox(Rect *theBox) {
  187.     long response;
  188.     Rect globalBox;
  189.     GDHandle maxDevice;
  190.     RGBColor rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF}, rgbBlack = {0, 0, 0}, sForground, sBackground;
  191.     PenState penSave;
  192.     Pattern qdgray, qdblack;
  193.         /* save the current drawing state */
  194.     GetPenState(&penSave);
  195.         /* if no color quickdraw, fail...*/
  196.     if (Gestalt(gestaltQuickdrawVersion, &response) != noErr) response = 0;
  197.     if (response >= gestalt8BitQD) {
  198.             /* get the device for the rectangle */
  199.         globalBox = *theBox;
  200.         LocalToGlobal((Point*) &globalBox.top);
  201.         LocalToGlobal((Point*) &globalBox.bottom);
  202.         maxDevice = GetMaxDevice(&globalBox);
  203.         if (maxDevice != NULL) {
  204.                 /* calculate the best gray */
  205.             if ( GetGray(maxDevice, &rgbWhite, &rgbBlack)) {
  206.                     /* draw over the area in gray using addMax transfer mode */
  207.                 GetForeColor(&sForground);
  208.                 GetBackColor(&sBackground);
  209.                 RGBForeColor(&rgbBlack);
  210.                 RGBBackColor(&rgbWhite);
  211.                 PenMode(addMax);
  212.                 PenPat(GetQDGlobalsBlack(&qdblack));
  213.                 PaintRect(theBox);
  214.                 RGBForeColor(&sForground);
  215.                 RGBBackColor(&sBackground);
  216.                     /* restore the pen state and leave */
  217.                 SetPenState(&penSave);
  218.                 return;
  219.             }
  220.         }
  221.     }
  222.         /* fall through to using the gray pattern */
  223.     GetQDGlobalsGray(&qdgray);
  224.     PenPat(&qdgray);
  225.     PenMode(notPatBic);
  226.     PaintRect(theBox);
  227.     SetPenState(&penSave);
  228. }
  229.  
  230.  
  231. /* RgnUnionRect adds the rectangle into the region. This routine works
  232.     by creating a temporary rectangular region equivalent to srcRect
  233.     and then calling UnionRgn. It's purpose is simply to stand in as a
  234.     shorthand for that sequence of calls.  */
  235. void RgnUnionRect(RgnHandle theRegion, Rect* srcRect) {
  236.     RgnHandle temp;
  237.     temp = NewRgn();
  238.     RectRgn(temp, srcRect);
  239.     UnionRgn(theRegion, temp, theRegion);
  240.     DisposeRgn(temp);
  241. }
  242.  
  243.  
  244. /* SetScrollBarCursor sets the cursor according to the mouse position
  245.     over a scroll bar.  If the cursor is over the scroll bar, then the routine
  246.     will return true and it will set the region mouseRgn to a region where
  247.     the mouse should remain as it has been set.  This region will be in global
  248.     coordinates suitable for passing to WaitNextEvent in the mouse region
  249.     parameter. */
  250. Boolean SetScrollBarCursor(ControlHandle scrollBar, Point where, RgnHandle mouseRgn) {
  251.     Rect bounds;
  252.     GetControlBounds(scrollBar, &bounds);
  253.     if (PtInRect(where, &bounds)) {
  254.         RgnHandle temp;
  255.         temp = NewRgn();
  256.         GetControlRegion(scrollBar, kControlIndicatorPart, temp);
  257.         if (PtInRgn(where, temp)) {
  258.             CopyRgn(temp, mouseRgn);
  259.             SetThemeCursor(kThemeOpenHandCursor);
  260.         } else {
  261.             RectRgn(mouseRgn, &bounds);
  262.             XorRgn(mouseRgn, temp, mouseRgn);
  263.             SetThemeCursor(kThemePointingHandCursor);
  264.         }
  265.         DisposeRgn(temp);
  266.         return true;
  267.     } else return false;
  268. }
  269.  
  270.  
  271. /* GetMenuBarRegion sets the region mBarRgn to the global coordinates
  272.     of the menu bar. */
  273. void GetMenuBarRegion(RgnHandle mBarRgn) {
  274.     GDHandle mainDevice;
  275.     Rect bounds;
  276.     mainDevice = GetMainDevice();
  277.     bounds = (**mainDevice).gdRect;
  278.     bounds.bottom = bounds.top + GetMBarHeight();
  279.     RectRgn(mBarRgn, &bounds);
  280. }
  281.  
  282.